<!-- 收货验收 -->
<template>
  <div>
    <el-card style="width: 98%; margin: 0 auto; margin-top: 10px">
      <div class="top">
        <div class="left">
          <div class="box">
            <div class="text">创建时间:</div>
            <div class="rgt">{{ dayjs(purchaseData?.addTime).format("YYYY-MM-DD") }}</div>
          </div>
          <div class="box">
            <div class="text">申请人:</div>
            <div class="rgt">{{ purchaseData?.addAccountName }}</div>
          </div>
          <div class="box">
            <div class="text">品种数:</div>
            <div class="rgt">{{ purchaseData?.counts }}</div>
          </div>
          <div class="box">
            <div class="text">实际采购成本:</div>
            <div class="rgt">{{ purchaseData?.addAccountId }}</div>
          </div>
        </div>
        <div v-if="isShow" class="deliver">已经发货</div>
      </div>

      <el-table :data="TableData" style="width: 100%">
        <el-table-column prop="id" label="序号" />
        <el-table-column prop="name" label="物料名称" />
        <el-table-column prop="unit" label="单位" />
        <el-table-column prop="supplierName" label="供应商" />
        <el-table-column prop="wholePrice" label="批发价" />
        <el-table-column prop="sellPrice" label="零售价" />
        <el-table-column prop="purchasePrice" label="采购价" />
        <el-table-column prop="purchaseCounts" label="采购数量" />
        <el-table-column prop="receiveCounts" label="采购实际数量" />
      </el-table>

      <div class="end">
        <div class="txt">到货凭证</div>
        <img v-if="isShow" :src="imgUrl + form.picture" class="avatar" />
        <el-upload
          v-if="!isShow"
          class="avatar-uploader"
          :action="baseUrl"
          :headers="headers"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.picture" :src="imgUrl + form.picture" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>
      </div>
    </el-card>

    <div class="btn">
      <el-button @click="back">返回</el-button>
      <el-button v-if="!isShow" type="primary" @click="submit">确认验收</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { purchaseReceive, purchaseFoods, purchaseGet } from "@/api/logistics/purchase/purchase";
import { purchaseListData, purchaseCheck } from "@/api/logistics/purchase/type";
import { dayjs, type UploadProps } from "element-plus";
import { getToken } from "@/utils/auth";

const router = useRouter();
const route = useRoute();
const id: number = route.query.id as unknown as number;

const form = reactive<purchaseCheck>({
  id: id,
  picture: "",
});

// 获取表格信息
const TableData = ref<purchaseListData[]>([]);

const getListData = async () => {
  let res = await purchaseFoods(id);
  // console.log('', res);
  TableData.value = res.list;
};

// 获取采购信息
const isShow = ref(false);
const purchaseData = ref<purchaseListData>();
const getData = async () => {
  let res = await purchaseGet(id);
  // console.log('采购信息111', res);
  purchaseData.value = res;
  form.picture = res.picture;
  if (res.state === "已经发货") {
    isShow.value = true;
  }
};

// 确认验收
const submit = async () => {
  await purchaseReceive(form).catch(() => {});
  // console.log('验收', res);
  ElMessage.success("已验收");
  router.push("/logistics/purchase");
};

// 返回
const back = () => {
  router.push("/logistics/purchase");
};

// 图片上传
const baseUrl = import.meta.env.VITE_APP_API_URL + "/api/upload/add"; // 图片根路径
const imgUrl = import.meta.env.VITE_IMG_URL; //获取图根路径

const headers = { Authorization: getToken() };

const handleAvatarSuccess: UploadProps["onSuccess"] = (response, uploadFile) => {
  form.picture = response.data.url;
};
const beforeAvatarUpload: UploadProps["beforeUpload"] = (rawFile) => {
  if (
    rawFile.type !== "image/jpeg" &&
    rawFile.type !== "image/png" &&
    rawFile.type !== "image/webp"
  ) {
    ElMessage.error("只能上传jepg/png/webp格式的图片!");
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error("上传的图片大小不能超过2MB!");
    return false;
  }
  return true;
};

onMounted(() => {
  getData();
  getListData();
});
</script>

<style lang="less" scoped>
.top {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;

  .left {
    .box {
      display: flex;
      margin-bottom: 5px;

      .text {
        width: 200px;
        font-size: 14px;
      }

      .rgt {
        font-size: 14px;
      }
    }
  }

  .deliver {
    font-size: 32px;
    margin: 20px;
  }
}

.end {
  margin-top: 30px;

  .txt {
    margin-bottom: 10px;
  }
}

.avatar {
  width: 80px;
  height: 80px;
}

.btn {
  display: flex;
  justify-content: center;
  margin: 30px 0;
}
</style>

<style>
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  width: 80px;
  height: 80px;
  font-size: 28px;
  color: #8c939d;
  text-align: center;
}
</style>
